Categories
JavaScript

Good Parts of JavaScript — Methods From the Standard Library

Spread the love

JavaScript is one of the most popular programming languages in the world. It can do a lot and have some features that are ahead of many other languages.

In this article, we’ll look at some useful array methods from the JavaScript standard library.

Array

The JavaScript standard library has many useful array methods.

array.unshift(item…)

The unshift adds items to the beginning of the array.

It returns the array’s new length.

For instance, we can write:

const a = ['a', 'b', 'c'];
const b = a.unshift('d');

Then a becomes [“d”, “a”, “b”, “c”] and b is 4.

Function

If we have to work with traditional functions, then these methods would help us.

function.apply(thisArg, argArray)

We can use apply to call a traditional function with a different value of this than what’s inside with an array of arguments.

For instance, we can write:

function foo(a, b) {
  console.log(`${this.value} ${a} ${b}`);
}

foo.apply({
  value: 'foo'
}, ['bar', 'baz']);

Then we get:

foo bar baz

from the console log output because this is:

{
  value: 'foo'
}

a is 'bar' and b is 'baz' .

Number

Various number methods also make our lives easier when we work with numbers.

number.toExponential(fractionDigits)

The toExponential method lets us convert numbers to exponential notation with the number of fractional digits after the decimal point.

The returned value is a string.

For instance, we can write:

console.log((Math.PI.toExponential(2)));

We get:

3.14e+0

from the console log output.

number.toFixed(fractionDigits)

The toFixed method returns a string representation of a number in decimal form.

It’ll have the number of digits after the decimal point as we specify them.

For instance, we can write:

console.log(Math.PI.toFixed(5));

And we get:

3.14159

from the console log.

number.toPrecision(precision)

The toPrecision method converts a number to a string in decimal form.

The precision parameter is optional and controls the number of digits of precision.

For instance, we can write:

console.log(Math.PI.toPrecision(5));

And get:

3.1416

number.toString(radix)

toString converts number to a string.

We can specify the optional radix parameter to control the base of the number.

radix should be between 2 and 36. The default radix is 10.

For instance, we can write:

console.log(Math.PI.toString(8));

to convert Math.PI to base 8. We get:

3.1103755242102643

Object

The Object.prototype has some useful methods that we can use.

object.hasOwnProperty(name)

hasOwnProperty lets us check if a property of an object is inherited or own property.

true is returned if name is an own property.

For instance, we can write:

const obj = {
  foo: 'bar'
};
const child = Object.create(obj);
console.log(child.hasOwnProperty('foo'));

Then we get false since foo is an inherited property of child .

RegExp

The RegExp constructor also comes with some useful methods for matching patterns.

regexp.exec(string)

The exec method finds all the matches of a regex pattern in a given string.

For instance, we can write:

console.log(/foo/g.exec('foo foo bar'));

Then we get the matches in an iterator.

regexp.test(string)

The test method is used for checking if a string matches the given regex.

For instance, we can write:

console.log(/foo/g.test('foo foo bar'));

And get true logged because 'foo' is in ‘foo foo bar’ .

String

Various string methods are also very useful for manipulating strings.

string.charAt(pos)

The charAt method returns the character at the given position.

For instance, we can write:

const name = 'joe';
const initial = name.charAt(0);

and get 'j' as the value of initial .

string.charCodeAt(pos)

charCodeAt is similar to charAt except that the integer representation of the code point of the character is returned instead of the character itself.

For instance, we can write:

const name = 'joe';
const code = name.charCodeAt(0);

Then we get code 106 for character 'j' .

If pos is less than 0 or bigger than or equal to string.length then NaN is returned.

string.concat(string…)

We can use the concat method to concatenate one or more strings together.

For instance, we can write:

console.log('foo'.concat('bar', 'baz'));

and get foobarbaz from the console log output.

Conclusion

There’re many array, strings, and regex methods that we can’t overlook.

They’re very useful and let us deal with any kind of data with ease.

Combining strings with concat is useful.

We can use the regex test method to check if a string match a regex pattern, and do much more.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *